if(typeof(SYS.Form)=='undefined')
{
	SYS.Form = function(id, options, success)
	{
		var that = this;
		that.options = $.extend({
			target:false,
			title:'Vict',
			drag:true,
			resize:true,
			module:SYS.blocks[SYS.currentBlockId]
		}, options);
		that.id = id;
		that.isShow = false;
		if(that.options['rules'])
		{
			that.rules = this.options['rules'];
		}
		if(that.options['messages'])
		{
			that.messages = that.options['messages'];
		}
		SYS.load('3rdparty/jQuery/jquery.ui/themes/'+SYS.UITheme+'/ui.all.css');
		SYS.load('3rdparty/jQuery/jquery.ui/ui/ui.core.js', function(){
			SYS.load('3rdparty/jQuery/jquery.ui/ui/ui.dialog.js', 		 
				'3rdparty/jQuery/plugins/jquery.form.js', function(){
				if(that.options['content'])
				{
					that.draw(this.options['content']);
				}
				
				if(typeof(success) == 'function')
				{
					success.call(this);
				}
				that = null;
			});
		});
	}
	SYS.Form.prototype = {
		rules:{},
		messages:{},
		init:function()
		{
			var currentForm = this;
			SYS.load('3rdparty/jQuery/plugins/jquery.validate/jquery.validate.min.js', function(){
				$('#'+currentForm.id).validate({
					rules:currentForm.rules,
					messages:currentForm.messages,
					errorElement:'div',
					submitHandler: function(form) {
						jQuery(form).ajaxSubmit({
							target : currentForm.options.target,
							success : function(responseText, statusText){currentForm.onSubmit(responseText, statusText);currentForm = null;},
							clearForm: true
						});
					}
				});
			});
		},
		draw:function(content)
		{
			if($('#modal'+this.id).length == 0)
			{
				//this.options.target
				var code = '<div id="modal'+this.id+'" style="display:none" title="'+(this.options.title?this.options.title:'')+'">'+(content?content.replace(/<\/form/,this.options.module.formSignature()+'</form'):'Đang kết nối... <img src="'+rootURL+'3rdparty/jQuery/plugins/jqModal/busy.gif" alt="loading" />')+'</div>';
				var parentObj = $('#Splitter');
				if(parentObj.length == 0)
				{
					$('body').prepend(code);
				}
				else
				{
					parentObj.append(code);
				}
				parentObj = null;
			}
		},
		show:function(href)
		{
			var form = $('#modal'+this.id);
			
			if(href && (typeof(href) == 'string'))
			{
				var currentForm = this;
				form.load(href, null, function(){
					currentForm.showDialog();
					currentForm.init();
					currentForm = null;					
				});
			}
			else
			{
				this.showDialog();
				this.init();
			}
			form = null;
		},
		showDialog:function()
		{
			var form = $('#modal'+this.id);
			form.show();
			this.isShow = true;
			if(form.parent().hasClass('ui-dialog'))
			{
				form.dialog('open');
			}
			else
			{
				form.dialog({
					modal:true,
					height:'auto',
					width:'auto'
				});	
			}
			form = null;
		},
		hide:function()
		{
			if(this.isShow)
			{
				this.isShow = false;
				$('#modal'+this.id).dialog('close');
			}
		},
		onSubmit: function (responseText, statusText)  
		{
			if(this.options['submit'])
			{
				this.options['submit'](responseText, statusText);
			}
		}
	}
}